﻿
namespace Apache.Cassandra.Client.Diagnostics {
    using System.Diagnostics;

    sealed internal class CassandraClientFactoryCounters {

        private static class CreationData {
            internal const string CategoryName = "NetCassa Client";
            internal const string CategoryHelp = "Counters for Apache.Cassandra.Client";

            static internal readonly CounterCreationData NumberOfActiveConnectionPools = new CounterCreationData("Connection Pools Active", "Number of active cluster pools.", PerformanceCounterType.NumberOfItems32);
            static internal readonly CounterCreationData NumberOfInactiveConnectionPools = new CounterCreationData("Connection Pools Inactive", "Number of active cluster pools.", PerformanceCounterType.NumberOfItems32);

            static internal readonly CounterCreationData NumberOfNonPooledConnections = new CounterCreationData("Connections Non-Pooled", "Number of connections not using connection pooling.", PerformanceCounterType.NumberOfItems32);

            static internal readonly CounterCreationData HardConnectsPerSecond = new CounterCreationData("Hard Connects per Second", "Number of non-pooled connections opened per second.", PerformanceCounterType.RateOfCountsPerSecond32);
            static internal readonly CounterCreationData HardDisconnectsPerSecond = new CounterCreationData("Hard Disconnects per Second", "Number of non-pooled connections closed per second.", PerformanceCounterType.RateOfCountsPerSecond32);
        }

        internal readonly CassandraCounter NumberOfActiveConnectionPools;
        internal readonly CassandraCounter NumberOfInactiveConnectionPools;

        internal readonly CassandraCounter NumberOfNonPooledConnections;

        internal readonly CassandraCounter HardConnectsPerSecond;
        internal readonly CassandraCounter HardDisconnectsPerSecond;

        internal CassandraClientFactoryCounters(string instanceName) {
            NumberOfActiveConnectionPools = new CassandraCounter(CreationData.CategoryName, instanceName, CreationData.NumberOfActiveConnectionPools);
            NumberOfInactiveConnectionPools = new CassandraCounter(CreationData.CategoryName, instanceName, CreationData.NumberOfInactiveConnectionPools);

            NumberOfNonPooledConnections = new CassandraCounter(CreationData.CategoryName, instanceName, CreationData.NumberOfNonPooledConnections);

            HardConnectsPerSecond = new CassandraCounter(CreationData.CategoryName, instanceName, CreationData.HardConnectsPerSecond);
            HardDisconnectsPerSecond = new CassandraCounter(CreationData.CategoryName, instanceName, CreationData.HardDisconnectsPerSecond);
        }

        static internal void InstallCounters() {
            CounterCreationDataCollection counterData = new CounterCreationDataCollection();

            counterData.Add(CreationData.NumberOfActiveConnectionPools);
            counterData.Add(CreationData.NumberOfInactiveConnectionPools);

            counterData.Add(CreationData.NumberOfNonPooledConnections);
            counterData.Add(CreationData.HardConnectsPerSecond);
            counterData.Add(CreationData.HardDisconnectsPerSecond);

            PerformanceCounterCategory.Create(CreationData.CategoryName, CreationData.CategoryHelp, PerformanceCounterCategoryType.MultiInstance, counterData);
        }

        static internal void UninstallCounters() {
            if (PerformanceCounterCategory.Exists(CreationData.CategoryName)) {
                PerformanceCounterCategory.Delete(CreationData.CategoryName);
            }
        }
    }
}
